{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import classification_report,confusion_matrix\n",
    "import operator\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def knn(x_test,x_data,y_data,k):\n",
    "    x_data_size = x_data.shape[0]\n",
    "    #复制x_test\n",
    "    np.tile(x_test,(x_data_size,1))\n",
    "    #求差值\n",
    "    diffMat = np.tile(x_test,(x_data_size,1))-x_data\n",
    "    #求平方\n",
    "    sqdiffMat = diffMat**2\n",
    "    #求和\n",
    "    sqDistances = sqdiffMat.sum(axis=1)\n",
    "    #开方\n",
    "    distances = sqDistances**0.5\n",
    "    #排序\n",
    "    sortedDistances = distances.argsort()\n",
    "    classCount = {}\n",
    "    k = 5\n",
    "    for i in range(k):\n",
    "        #获取标签\n",
    "        votelabel = y_data[sortedDistances[i]]\n",
    "        #统计每个标签的个数\n",
    "        classCount[votelabel] = classCount.get(votelabel,0)+1\n",
    "    #根据operator.itemgetter(1)  -> 第一个值对classCount排序，然后取倒序\n",
    "    sortedclassCount = sorted(classCount.items(),key = operator.itemgetter(1),reverse = True)\n",
    "    return sortedclassCount[0][0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "iris = datasets.load_iris()\n",
    "x_train,x_test,y_train,y_test = train_test_split(iris.data, iris.target, test_size=0.2) #分割数据0.2为测试数据，0.8为训练数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0, 1, 0, 2, 2, 0, 0, 1, 1, 2, 0, 1, 1, 1, 1, 2, 0, 0, 2, 1, 2,\n",
       "       1, 0, 1, 0, 1, 2, 2, 1, 2, 2, 1, 2, 1, 0, 1, 0, 2, 2, 2, 1, 1, 1, 2,\n",
       "       2, 0, 2, 1, 0, 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0,\n",
       "       1, 1, 2, 0, 2, 1, 2, 0, 0, 0, 2, 0, 1, 1, 1, 2, 2, 1, 0, 2, 1, 0, 0,\n",
       "       1, 0, 1, 0, 1, 0, 1, 1, 0, 2, 2, 1, 2, 2, 2, 1, 2, 0, 2, 1, 2, 2, 2,\n",
       "       0, 2, 0, 1, 0])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       1.00      1.00      1.00         8\n",
      "          1       0.91      0.91      0.91        11\n",
      "          2       0.91      0.91      0.91        11\n",
      "\n",
      "avg / total       0.93      0.93      0.93        30\n",
      "\n"
     ]
    }
   ],
   "source": [
    "predictions =[]\n",
    "for i in range(x_test.shape[0]):\n",
    "    predictions.append(knn(x_test[i],x_train,y_train,5))\n",
    "print(classification_report(y_test,predictions))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1, 1, 0, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 0, 1, 1, 2, 0, 1, 1, 2,\n",
       "       0, 0, 1, 2, 1, 0, 0])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0,\n",
       " 1,\n",
       " 1,\n",
       " 1,\n",
       " 0,\n",
       " 2,\n",
       " 2,\n",
       " 1,\n",
       " 2,\n",
       " 1,\n",
       " 2,\n",
       " 2,\n",
       " 2,\n",
       " 2,\n",
       " 2,\n",
       " 0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 0,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 0,\n",
       " 0,\n",
       " 1,\n",
       " 2,\n",
       " 1,\n",
       " 0,\n",
       " 0]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
